package com.yowasa.xiuxian.engine.message.item;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.CommonEngine;
import com.yowasa.xiuxian.engine.ItemEngine;
import com.yowasa.xiuxian.engine.item.ItemInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.item.UserItemBo;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.QualityEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuXianDrawing;
import com.yowasa.xiuxian.entity.po.XiuxianUserEquip;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.ItemHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@CMD(name = "/炼金台", scope = CmdScopeEnum.ALL)
public class AlchemyHandler extends CmdMessageHandler {
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private ItemHelper itemHelper;
    @Autowired
    private ItemEngine itemEngine;



    @Override
    public void execute(SendMessage message) {
        UserBO user = userHelper.checkOpUser(message);
        List<String> params = getAllParam(message);

        if (StringUtils.isEmpty(params.get(0))) {
            messageHelper.replyMys(message, "可以在后面追加 图鉴 合成等二级指令进行后续操作");
            return;
        }
        if ("合成".equals(params.get(0))) {

            if(params.size() == 1){
                StringBuffer sb =new StringBuffer();
                sb.append("指令格式为 /炼金台 合成 物品名称 材料名称\n当一件物品有多种合成配方时，想指定配方，请加上配方中第一个材料的名称\n");
                messageHelper.replyMys(message, sb.toString());
                return;
            }
            String name = params.get(1);
            ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(name);
            //查找图纸，找出所需材料
            try {
                //选择
                String perName = null;
                if(params.size() >= 3){
                    perName = params.get(2);
                }
                //合成方式列表
                List<XiuXianDrawing> alchemy = itemHelper.getAlchemy(name);
                //合成校验
                Boolean flag = itemHelper.checkAlchemy(user,perName,alchemy,itemInterface);
                if(flag){
                    //可以合成，扣钱，给东西
                    user.getUserConfig().setGold(user.getUserConfig().getGold() - itemInterface.getPrice()/10);
                    userHelper.saveUserInfo(user);
                    //给东西
                    itemHelper.addItem(user,itemInterface,1);
                    messageHelper.replyMys(message,MessageFormat.format("合成成功，你获得了 {0} * 1",name));
                    return;
                }
            }catch (Exception e){
                throw new HandlerException(e.getMessage());
            }
        }
        if ("图鉴".equals(params.get(0))){
            List<XiuXianDrawing> drawingList = itemHelper.getAllAlchemy();
            StringBuffer sb = new StringBuffer();
            sb.append("指令 /炼金台 图鉴 查看所有可合成的物品\n/炼金台 图鉴 名称 查看具体所需材料\n\n");
            if (params.size() == 2){
                String name = params.get(1);
                List<XiuXianDrawing> collect = drawingList.stream().filter(i -> i.getEquipName().equals(name)).collect(Collectors.toList());
                for (XiuXianDrawing drawing : collect){
                    sb.append(drawing.getEquipName()).append("   ").append(drawing.getSucaiDesc()).append("\n");
                }
                messageHelper.replyMys(message,sb.toString());
            }else {
                for (XiuXianDrawing drawing : drawingList){
                    sb.append(drawing.getEquipName()).append("\n");
                }
                messageHelper.replyMys(message,sb.toString());
            }
        }
        return;
    }

    public String desc(){
        return new StringBuffer().append("炼金术实际上是研讨物质本质与其变化规律的学科，最终通向的是世界运转、地脉流通的规律\n")
                .append("可通过炼金台将部分材料转换形态，需要嬗变之尘和易梦溶媒")
                .toString();

    }
}
